package com.whsr.readservice.controller;

import com.alibaba.fastjson.JSONArray;
import com.whsr.readservice.service.CategoryService;
import com.whsr.readservice.service.PageConfigService;
import com.whsr.readservice.util.ResultUtil;
import com.whsr.readservice.util.TreeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("api/mobile/")
public class MobileIndexPageController {

    private final PageConfigService pageConfigService;
    private final CategoryService categoryService;

    public MobileIndexPageController(PageConfigService pageConfigService, CategoryService categoryService) {
        this.pageConfigService = pageConfigService;
        this.categoryService = categoryService;
    }

    /**
     * @author chatwin
     * @param type 传过来的类型标示，首页或者分类
     * @return
     */
    @RequestMapping("tab/{type}")
    @ResponseBody
    public Object getIndexTable(@PathVariable String type){
        try {
            List<Map<String, Object>> results = pageConfigService.getIndexTab(type);
            return ResultUtil.getResultBySuccess(results);
        }catch (Exception e){
            return ResultUtil.getResultByError();
        }

    }


    /**
     * @author chatwin
     * @param id 传过来的首页id
     * @return 返回当前页面的数据
     */
    @RequestMapping("index/{id}")
    @ResponseBody
    public Object mobileIndex(@PathVariable int id){
        try {
            List<Map<String, Object>> results = pageConfigService.getMobileIndex(id);
            List<Map<String, Object>> res = new ArrayList<>();
            for(int i = 0; i< results.size(); i++){
                Map<String, Object> item = results.get(i);
                List<Map<String, Object>> children = pageConfigService.getChildren(Integer.valueOf(item.get("id")+""), item.get("templet").toString());
                item.put("children", children);
                res.add(item);
            }
            return ResultUtil.getResultBySuccess(res);
        }catch (Exception e){
            return ResultUtil.getResultByError();
        }
    }


    /**
     * @author chatwin
     * @param resMap 页面传过来的参数
     * @return 返回分页信息
     */
    @RequestMapping("artPage")
    @ResponseBody
    public Object getArtByPage(@RequestBody Map<String, Object> resMap){
        try {
            Map<String, Object> results = pageConfigService.getArtByPage(resMap);
            return ResultUtil.getResultBySuccess(results);
        }catch (Exception e){
            return ResultUtil.getResultByError();
        }
    }


    @RequestMapping("category")
    @ResponseBody
    public Object getCategory(){
        try {
            List<Map<String, Object>> results = this.categoryService.getAllCategory();
            JSONArray ja = TreeUtil.voListToTree(results, "id", "pid", "0", "children");
            return ResultUtil.getResultBySuccess(ja);
        }catch (Exception e){
            return ResultUtil.getResultByError();
        }
    }


}
